import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
import {viteCommonjs} from '@originjs/vite-plugin-commonjs'

export default defineConfig(({command}) => {
    return {
        base: command === 'build' ? '/sw/' : './',
        plugins: [
            vue(),
            VueSetupExtend(),
            AutoImport({
                resolvers: [ElementPlusResolver()]
            }),
            Components({
                resolvers: [ElementPlusResolver()]
            }),
            viteCommonjs()
        ],
        build: {
            outDir: '../sw-admin/src/main/resources/static', // 🔥 关键修改：输出到Spring Boot的static目录
            assetsDir: 'static', // 静态资源存放子目录（可选）
            emptyOutDir: true // 构建前清空目标目录
        },
        resolve: {
            alias: {
                '@': '/src',
                '~': '/src/assets'
            }
        },
        define: {
            __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'true'
        },
        server: {
            proxy: {
                '/sw': {
                    target: 'http://localhost:2001/sw', // 目标 API 地址
                    changeOrigin: true, // 允许跨域
                    rewrite: (path) => path.replace(/^\/sw/, '') // 可选，重写路径
                }
            }
        }
    }
})
